package top.xzxsrq.fileviewutils.download;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;
import top.xzxsrq.utils.PathUtils;
import top.xzxsrq.utils.StringUtilsZX;
import top.xzxsrq.web.download.FeiJiSuD;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Component
@Data
public class FeiJiSuDownload extends FeiJiSuD {
    private String saveDir = "saveDir"; // 文件保存位置
    private String videoStart = "videoStart"; // 视频开始位置
    private String videoEnd = "videoEnd"; // 视频结束位置
    private String nameLength = "nameLength"; // 名字的长度
    private String templatePageUrl = "templatePageUrl"; // 下载模板url http://m.feijisu36.com/zongyi/90182/${}.html${}
    private String initPageQuery = "initPageQuery"; // ?qp=ff

    @Override
    public void webRunFunc(String downloadTaskId, String userBasePath, JSONObject jsonObject) throws Exception {
        String saveDir = jsonObject.getString(this.saveDir);
        saveDir = PathUtils.pathResolve(userBasePath, saveDir);
        FeiJiSuDownload feiJiSuD = new FeiJiSuDownload();
        feiJiSuD.downloadTaskId = downloadTaskId;
        feiJiSuD.downloadPages(
                saveDir,
                jsonObject.getInteger(videoStart),
                jsonObject.getInteger(videoEnd),
                jsonObject.getInteger(nameLength),
                jsonObject.getString(templatePageUrl),
                jsonObject.getString(initPageQuery)
        );
    }

    @Override
    public List<Map<Object, Object>> webRunFuncParams() {
        List<Map<Object, Object>> result = new LinkedList<>();
        result.add(webRunFuncParam(
                "prop",
                "",
                "label",
                "下载说明",
                "type",
                "downloadInstructions",
                "value",
                "这是飞极速下载\n" +
                        "下载模板url http://m.feijisu36.com/zongyi/90182/${}.html${}\n" +
                        "第一个${}表示替换的是视频开始位置到结束位置\n" +
                        "第二个${}替换的是视频参数\n" +
                        "也就是初始url的参数"
        ));
        result.add(webRunFuncParam(
                "prop",
                saveDir,
                "label",
                "保存到文件夹",
                "type",
                "SelectBasePath"
        ));
        result.add(webRunFuncParam(
                "prop",
                videoStart,
                "label",
                "视频开始",
                "type",
                "inputNumber"
        ));
        result.add(webRunFuncParam(
                "prop",
                videoEnd,
                "label",
                "视频结束",
                "type",
                "inputNumber"
        ));
        result.add(webRunFuncParam(
                "prop",
                nameLength,
                "label",
                "名字长度",
                "type",
                "inputNumber"
        ));
        result.add(webRunFuncParam(
                "prop",
                templatePageUrl,
                "label",
                "模板url",
                "type",
                "input",
                "props",
                webRunFuncParam(
                        "type",
                        "textarea",
                        "autosize",
                        "true"
                )
        ));
        result.add(webRunFuncParam(
                "prop",
                initPageQuery,
                "label",
                "初始url参数",
                "type",
                "input",
                "initValue", // 给一个初始化值
                "?qp=ff"
        ));
        return result;
    }

    @Override
    public JSONObject urlParse(JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        // http://m.feijisu36.com/zongyi/90182/1.html?qp=ff
        String url = jsonObject.getString(templatePageUrl);
        if(StringUtilsZX.isEmpty(url)) {
            return new JSONObject();
        }
        url = StringUtilsZX.replace(url, "(.*?/)(\\d+)\\.html(.*)", (reg) -> {
            if (reg.size() > 3) {
                result.put(initPageQuery,reg.get(3));
            }
            result.put(videoStart,Integer.valueOf(reg.get(2)));
            return reg.get(1) + "${}.html${}";
        });
        result.put(templatePageUrl, url);
        result.put(nameLength, 4);
        return result;
    }

//    public static void main(String[] args) {
//        FeiJiSuDownload feiJiSuDownload = new FeiJiSuDownload();
//        JSONObject test = new JSONObject();
//        test.put("templatePageUrl", "http://m.feijisu36.com/zongyi/90182/1.html?qp=ff");
//        System.out.println("test = " + test);
//        System.out.println("feiJiSuDownload.urlParse(test) = " + feiJiSuDownload.urlParse(test));
//    }
}
